//-----------------------------------------------------------------------
//  This file is part of the Microsoft Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.Samples.SqlServer
{
    public class Logger 
    {
        public void StoreProcessingInfo(ArrayList list, SqlConnection conn, SqlTransaction tran)
        {
            foreach (LogItem logItem in list)
            {
                string id = logItem.SalesOrderId;

                if (id.Equals(""))
                {
                    id = "NA";
                }

                string information = "" + id + " \\ " + logItem.FunctionName + " \\ " + logItem.Message;

                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "StoreProcessingInfo";
                cmd.Transaction = tran;

                // Mark the Command as a SPROC
                cmd.CommandType = CommandType.StoredProcedure;

                // Add Parameters to SPROC
                SqlParameter paramInformation = new SqlParameter("@Information", SqlDbType.NVarChar, 2000);

                paramInformation.Value = information;
                cmd.Parameters.Add(paramInformation);
                cmd.ExecuteNonQuery();
            }

            list = new ArrayList();
            return;
        }
    }

    public class LogItem
    {
        private string functionName;
        private string salesOrderId;
        private string message;

        public LogItem(string name, string id, string msg)
        {
            functionName = name;
            salesOrderId = id;
            message = msg;
        }

        public string FunctionName
        {
            get
            {
                return functionName;
            }
            set
            {
                functionName = value;
            }
        }

        public string SalesOrderId
        {
            get
            {
                return salesOrderId;
            }
            set
            {
                salesOrderId = value;
            }
        }
        
        public string Message
        {
            get
            {
                return message;
            }
            set
            {
                message = value;
            }
        }
    }
}
